﻿#pragma execution_character_set("utf-8")
#include "TcpClient.h"
#include <QHostAddress>
#include <QHBoxLayout>

TcpClient::TcpClient(QString ip, int port, QWidget* parent)
	:QWidget(parent)
{
	this->port = port;
	this->ip = ip;
	lb_port = new QLabel(QString::number(port));
	initGUI();
	initServer(ip,port);
}

TcpClient::~TcpClient()
{
	socket->close();
}

void TcpClient::initServer(QString ip, int port)
{
	socket = new QTcpSocket(this);
	socket->connectToHost(QHostAddress(ip), port);
	lb_port->setText("未连接");
	timer.start(20);
	connect(&timer, &QTimer::timeout, [=]() 
		{
			socket->connectToHost(QHostAddress(ip), port);
			t++;
			lb_status->setText("已断开,尝试重新连接"+QString::number(t/50)+"s");
			if (t >= 300)	//6s重新连接
			{
				timer.stop();
				lb_status->setText("已断开");
				btn_stop->setEnabled(false);
				btn_start->setEnabled(true);
				t = 0;
			}
		});
	connect(socket, &QTcpSocket::connected, [=]()
		{
			lb_status->setText("已连接");
			lb_port->setText(QString::number(socket->localPort()));
			btn_send->setEnabled(true);
			timer.stop();
		});
	connect(socket, &QTcpSocket::disconnected, [=]() //断开重连
		{
			lb_status->setText("已断开,尝试重新连接...");
			lb_port->setText("未连接");
			btn_send->setEnabled(false);
			t = 0;
			timer.start(20);
		});
	connect(socket, &QTcpSocket::readyRead, this, &TcpClient::On_Received);
}

void TcpClient::initGUI()
{
	/* 初始化GroupBox */
	mainLayout = new QGridLayout(this);
	top_layout = new QGridLayout();
	QHBoxLayout* top1 = new QHBoxLayout();
	QHBoxLayout* top2 = new QHBoxLayout();
	center_layout = new QGridLayout();
	bottom_layout = new QGridLayout();
	topBox = new QGroupBox("Socket状态");
	centerBox = new QGroupBox("数据接收与提示窗口");
	bottomBox = new QGroupBox("数据发送窗口");
	topBox->setLayout(top_layout);
	centerBox->setLayout(center_layout);
	bottomBox->setLayout(bottom_layout);
	mainLayout->addWidget(topBox);
	mainLayout->addWidget(centerBox);
	mainLayout->addWidget(bottomBox);
	mainLayout->setRowStretch(1, 1);
	mainLayout->setRowStretch(2, 1);

	/* 初始化控件 */
	lb_status = new QLabel("已断开");//记录Socket状态
	lb_rip = new QLabel(ip);//远程IP
	lb_rport = new QLabel(QString::number(port));//远程端口
	btn_start = new QPushButton("连接");
	btn_start->setEnabled(false);
	btn_stop = new QPushButton("断开");
	connect(btn_start, &QPushButton::clicked, this, &TcpClient::On_Start);
	connect(btn_stop, &QPushButton::clicked, this, &TcpClient::On_Stop);
	top_layout->addLayout(top1,0,0,1,1);
	top_layout->addLayout(top2, 1, 0,1,1);

	top1->addWidget(lb_status,0,Qt::AlignLeft);
	top1->addStretch(1);
	top1->addWidget(new QLabel("对方地址："), 0, Qt::AlignRight);
	top1->addWidget(lb_rip, 0, Qt::AlignRight);
	top1->addWidget(new QLabel("对方端口："),  0, Qt::AlignRight);
	top1->addWidget(lb_rport,  0, Qt::AlignRight);
	


	top2->addWidget(btn_start, 0, Qt::AlignLeft);
	top2->addWidget(btn_stop, 0, Qt::AlignLeft);
	top2->addStretch(1);
	top2->addWidget(new QLabel("本地端口："), 0, Qt::AlignRight);
	top2->addWidget(lb_port, 0, Qt::AlignRight);

	top_layout->setSpacing(10);

	t_receive = new QTextEdit();
	t_receive->setReadOnly(true);
	center_layout->addWidget(t_receive);

	t_send = new QTextEdit();
	btn_send = new QPushButton("发送");
	btn_send->setEnabled(false);
	connect(btn_send, &QPushButton::clicked, this, &TcpClient::On_Send);
	bottom_layout->addWidget(t_send, 0, 0, 2, 1);
	bottom_layout->addWidget(btn_send, 0, 1);
	bottom_layout->setRowStretch(1, 1);
}

void TcpClient::On_Stop()
{
	socket->close();//先关闭Socket
	timer.stop();
	t = 0;
	lb_status->setText("已断开");
	btn_start->setEnabled(true);
	btn_stop->setEnabled(false);
	btn_send->setEnabled(false);
}

void TcpClient::On_Start()
{
	socket->connectToHost(QHostAddress(ip), port);
	timer.start(20);
	btn_start->setEnabled(false);
	btn_stop->setEnabled(true);
}

void TcpClient::On_Received()
{
	QByteArray str = socket->readAll();
	t_receive->append(ip + ":" + QString::number(port) + "【接收】: " + str);
}

void TcpClient::On_Send()
{
	t_receive->append(ip + ":" + QString::number(port) + "【发送】: " + t_send->toPlainText().toUtf8());
	socket->write(t_send->toPlainText().toUtf8());
}
